package com.augn.handler;

import com.augn.utils.TokenUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private TokenUtil tokenUtil;
    @Override
    public void insertFill(MetaObject metaObject) {
        String userId = tokenUtil.getUserId();
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        if(userId==null) {
            this.strictInsertFill(metaObject, "createBy", String.class,"self");
            this.strictInsertFill(metaObject, "updateBy", String.class, "self");
        } else {
            this.strictInsertFill(metaObject, "createBy", String.class,userId);
            this.strictInsertFill(metaObject, "updateBy", String.class, userId);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String userId = tokenUtil.getUserId();
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "updateBy", String.class, userId);
    }
}
